<template>
  <div>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button
        >
      </el-col>
    </el-row>

    <el-table
      :data="methodList"
      border
      style="width: 100%"
    >
      <!--      <el-table-column-->
      <!--        prop="id"-->
      <!--        label="ID"-->
      <!--        align="center"-->
      <!--        width="180">-->
      <!--      </el-table-column>-->
      <el-table-column
        prop="name"
        label="方法名称"
        align="center"
        width="180"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="方法地址"
        prop="reqMain"
      >
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="method.title" :visible.sync="method.open" @close="close"  width="700px" append-to-body>

      <el-form :model="funcForm" class="addDialog" :rules="methodRules" ref="funcDialog" label-width="120px">
        <el-form-item label="请求方法名称" prop="name">
          <el-input v-model="funcForm.name"></el-input>
        </el-form-item>

        <el-form-item label="请求方法地址" prop="reqMain">
          <el-input v-model="funcForm.reqMain"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFuncForm()">确 定</el-button>
        <el-button @click="close">取 消</el-button>
      </div>

    </el-dialog>
  </div>
</template>

<script>

import {
  listSupplierRequest,
  getSupplierRequest,
  addSupplierRequest,
  updateSupplierRequest,
  delSupplierRequest
} from '@/api/pm/supplierRequest';
import {listSupplierMain} from "@/api/pm/supplierMain";

export default {
  name: 'RequestMethod',
  data() {
    return {
      methodList: [],
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      method: {
        title: '新增请求方法',
        open: false
      },
      funcForm:{
        name: '',
        reqMain: ''
      },
      methodRules: {
        name: [
          {required: true, message: '请求方法名称不能为空', trigger: 'blur'}
        ],
        reqMain: [
          {required: true, message: '请求方法地址不能为空', trigger: 'blur'}
        ]
      }

    }
  },
  props: {
    "supplierId": String
  },
  watch: {
    'supplierId' :{
      handler(val) {
        this.getList();
      },
      immediate: true
    }
  },
  methods: {
    getList() {
      // console.log('获取list操作', this.queryParams);

      let param = {
        "supplierId": this.supplierId,
        "pageNum": this.queryParams.pageNum,
        "pageSize": this.queryParams.pageSize
      };

      listSupplierRequest(param).then(res => {
        if (res.code == 200) {
          this.methodList = res.rows;
          this.total = res.total;
        }
      }).catch(error => {
        // console.log(error);
      });
    },

    // 新增
    handleAdd() {
      this.funcForm.supplierId = this.supplierId;

      this.funcForm.name      = '';
      this.funcForm.reqMain   = '';
      this.funcForm.id        = '';

      this.method.open = true;
    },

    // 修改
    handleUpdate(row) {

      this.funcForm.supplierId  = row.supplierId;
      this.funcForm.name        = row.name;
      this.funcForm.reqMain     = row.reqMain;
      this.funcForm.id          = row.id;

      getSupplierRequest(row.id).then(res=>{
        if(res.code == 200) {
          let data = res.data;
          this.funcForm.supplierId = data.supplierId;
          this.funcForm.name       = data.name;
          this.funcForm.reqMain    = data.reqMain;
        }
      }).catch(error=>{
        // console.log(error);
      });

      this.method.title = '修改请求方法';
      this.method.open = true;
    },

    // 删除
    handleDelete(row) {
      this.$modal.confirm('确定删除该方法吗?')
      .then(()=>{
        return delSupplierRequest(row.id);
      })
      .then(res=>{
        let type = res.code == 200 ? 'success' : 'error';
        this.messageMethods(res.msg, type);
        this.getList();
      })
      .catch(()=>{});
    },

    // 提交
    submitFuncForm() {
      this.$refs['funcDialog'].validate((valid)=>{
        if(valid) {
          if(this.funcForm.id) {
            this.editMethod();
          } else {
            this.addMethod();
          }
        }
      });
    },

    editMethod() {
      updateSupplierRequest(this.funcForm).then(res=>{
        let type = res.code == 200 ? 'success' : 'error';
        this.messageMethods(res.msg, type);
        this.method.open = false;
        this.$refs.funcDialog.clearValidate();
        this.getList();
      }).catch(error=>{
        this.messageMethods(error, 'error');
        this.method.open = false;
        this.$refs.funcDialog.clearValidate();
        this.getList();
      });
    },

    addMethod() {
      addSupplierRequest(this.funcForm).then(res=>{
        let type = res.code == 200 ? 'success' : 'error';
        this.messageMethods(res.msg, type);
        this.method.open = false;
        this.getList();
      }).catch(error=>{
        this.messageMethods(error, 'error');
        this.method.open = false;
        this.getList();
      });
    },

    messageMethods(msg, type) {
      this.$message({
        message: msg,
        type: type
      });
    },

    close() {
      this.method.open = false;
      this.$refs.funcDialog.clearValidate();
    }
  }
}
</script>

<style>

</style>
